<template>
  <view class="my-category">
    <!-- 搜索框 -->
    <search />

    <view class="m-category">
      <scroll-view scroll-y class="left">
        <view
          class="item"
          v-bind:class="{ active: currentIndex === index }"
          v-for="(item, index) in leftList"
          :key="item.cat_id"
          @click="chooseFirstCate(index)"
          >{{ item.cat_name }}</view
        >
      </scroll-view>
      <scroll-view :scroll-top="scrollTop" scroll-y class="right">
        <view class="cate" v-for="item in rightList" :key="item.cat_id">
          <view class="title">{{ item.cat_name }}</view>
          <view class="goods-list">
            <view class="goods" v-for="goods in item.children" :key="goods.cat_id">
              <navigator
                :url="`/goodsPackage/goods_list/index?query=${goods.cat_name}&cid=${goods.cat_id}`"
              >
                <image :src="goods.cat_icon" class="pic" mode="aspectFit" />
                <view class="name">{{ goods.cat_name }}</view>
              </navigator>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>

    <!-- <view class="main"></view> -->
  </view>
</template>

<script>
export default {
  name: 'CategoryCom',
  data() {
    return {
      // count这个变量没有实际意义，辅助我们获取动态数据categoriesList。
      // 因为categoriesList这个变量没有绑定到data上面，不会被vue监听，没有被Object.defineProperty
      // 转换为get  set的形式。所以需要借助一个另外变量
      count: 1,
      // categoriesList: [],
      leftList: [],
      rightList: [],
      // 当前点击的是哪一个一级分类
      currentIndex: 0,
      scrollTop: 0,
    };
  },
  onLoad() {
    this.getCategory();
  },
  mounted() {
    this.scrollTop = 200;
  },
  // computed: {
  //   leftList() {
  //     if (this.categoriesList && Array.isArray(this.categoriesList)) {
  //       return this.categoriesList.map((item) => ({
  //         cat_id: item.cat_id,
  //         cat_name: item.cat_name,
  //       }));
  //     }

  //     return [];
  //   },
  // },
  watch: {
    count(newVal, oldVal) {
      if (this.categoriesList && Array.isArray(this.categoriesList)) {
        this.leftList = this.categoriesList.map((item) => ({
          cat_id: item.cat_id,
          cat_name: item.cat_name,
        }));
      }
    },
  },
  methods: {
    async getCategory() {
      this.count++;
      const res = await uni.$u.http.request({
        url: '/categories',
      });
      this.count++;
      // 这个代码是异步的代码，请求5S以后返回，5s之前this上面还没有这个属性， 开始的就是undeifned
      this.categoriesList = res.data.message;

      this.rightList = this.categoriesList[0].children;
    },
    chooseFirstCate(index) {
      this.scrollTop = -1;
      this.currentIndex = index;

      this.rightList = this.categoriesList[index].children;

      this.$nextTick(() => {
        // 等dom更新以后，再将scroll-top设置为0，回到顶部，一定要dom更新完
        this.scrollTop = 0;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.my-category {
  width: 750rpx;
  height: 100vh;
  background-color: antiquewhite;
}

.m-category {
  display: flex;
  height: calc(100vh - 39px);
  .left {
    height: 100%;
    width: 182rpx;
    font-size: 28rpx;
    text-align: center;
    background-color: #f3f3f3;
    .item {
      padding: 17rpx 0;
      &.active {
        background: #fff;
        color: #ea4350;
        position: relative;
        &::before {
          content: '';
          position: absolute;
          left: 0;
          width: 4rpx;
          height: 44rpx;
          background: #ea4350;
        }
      }
    }
  }
  .right {
    height: 100%;
    flex: 1;
    .title {
      margin: 18rpx 0 21rpx;
      text-align: center;
    }
  }
}
.goods-list {
  display: flex;
  padding: 0 48rpx;
  justify-content: space-between;
  flex-wrap: wrap;
  .pic {
    width: 120rpx;
    height: 120rpx;
  }
  .name {
    margin-bottom: 30rpx;
    font-size: 21rpx;
    color: #2e2e2e;
    text-align: center;
    line-height: 1;
  }
}

.main {
  width: calc(100% - 100px);
  height: calc(100vh - 50px - 20rpx);
  background-color: aqua;
}
</style>
